﻿@{
    Layout = null;
}
@using KingRoad.EPM.Common.Helper
<script type="text/javascript">
    $(function () {
        //计算分栏高度
        var contentHeight = $("#tabContent").innerHeight() - $(".panelBar", navTab.getCurrentPanel()).innerHeight();
        $("#mainContent_permission_duty_index").height(contentHeight);       

    });
    //树
    var zNodes_permission_duty_index = [];
    var zTree_permission_duty_index;
    var setting_permission_duty_index;
    var deleteID_permission_duty_index;
    var rootNodeId_permission_duty_index = "0"; //记录根节点id
    //全局的href
    var href_permission_duty_index = "/Permission/Duty";
    setting_permission_duty_index = {
        checkable: false,
        async: true,
        isSimpleData: true,
        editable: true,
        edit_renameBtn: false,
        edit_removeBtn: false,
        dragCopy: true,
        dragMove: true,
        asyncUrl: "/Permission/Duty/GetDutyNodes",  //获取节点数据的URL地址
        //asyncDataFilter: ajaxDataFilter,
        nameCol: "Name",
        open: false,
        treeNodeKey: "ID",
        treeNodeParentKey: "ParentId",
        callback: {
            //             beforeAsync: zTreeBeforeAsync,
            asyncSuccess: zTreeOnAsyncSuccess_permission_duty_index,
            //             asyncError: zTreeOnAsyncError,
            click: zTreeOnClick_permission_duty_index,
            //拖拽功能
            beforeDrop: zTreeBeforeDrop__Permission_duty_Index,
            drop: zTreeOnDrop__Permission_duty_Index
        }
    };

    function zTreeOnAsyncSuccess_permission_duty_index(event, treeId, treeNode, msg) {
        //异步成功
    }
    //树的单击事件
    function zTreeOnClick_permission_duty_index(event, treeId, treeNode) {
        //添加岗位
        var addDutyUrl_permission_duty_index = href_permission_duty_index + "/CreateDuty/" + treeNode.ID;
        $("#addDuty_permission_duty_index").attr("href", addDutyUrl_permission_duty_index);
        if (treeNode.ID != 0) {//点的不是根节点
            deleteID_permission_duty_index = treeNode.ID; //点击树节点就记录可能删除的节点id
            var nodeId = treeNode.ID;
            $("#rightDetail_permission_duty_index").load("/Permission/Duty/ShowDutyDetail/" + treeNode.ID, null, function () {
                //更新岗位
                var editDutyUrl_permission_duty_index0 = href_permission_duty_index + "/EditDuty/" + treeNode.ID;
                $("#editDuty_permission_duty_index").attr("href", editDutyUrl_permission_duty_index0);
            });
        }
    }

    $(document).ready(function () {
        refreshTree("/Permission/Duty/GetDutyNodes");
        //模块删除操作
        $("#del_permission_duty_index").click(function () {
            if (zTree_permission_duty_index.getSelectedNode()) {
                alertMsg.confirm("您确定要删除吗？", {
                    okCall: function () {
                        $.post("/Permission/Duty/Delete/" + deleteID_permission_duty_index, null, dialogdel_permission_duty_index, "json");
                    }
                });
            } else {
                alertMsg.error("请先选择要删除的项！");
                return false;
            }
        });
    });

    function zTreeBeforeDrop__Permission_duty_Index(treeId, treeNode, targetNode, moveType) {
        //移动前判断是否可移动
        if (treeNode.ParentId == targetNode.ID)
            return false;
        else if (treeNode.ParentId == 0 && targetNode.ID == 0)
            return false;
    }

    function zTreeOnDrop__Permission_duty_Index(event, treeId, treeNode, targetNode, moveType) {
        //移动
        if (treeNode.ID != 0) {
            $.post("/Permission/Duty/Move", { "Id": treeNode.ID, "pid": targetNode.ID }, dialogMove_Permission_duty_Index, "json");

            //            alertMsg.confirm("确定移动 " + treeNode.Name + " 到 " + targetNode.Name + "？", {
            //                okCall: function () {
            //                    $.post("/Permission/Duty/Move", { "Id": treeNode.ID, "pid": targetNode.ID }, dialogMove_Permission_duty_Index, "json");
            //                }
            //            });
        }
    }
    //拖拽的回调方法
    function dialogMove_Permission_duty_Index(json) {
        DWZ.ajaxDone(json);
        if (json.statusCode == DWZ.statusCode.ok) {
            //            var oldNode = zTree_permission_duty_index.getNodeByParam("ID", json.data.ID);
            //            var newParentNode = zTree_permission_duty_index.getNodeByParam("ID", json.data.ParentId);
            //            zTree_permission_duty_index.removeNode(oldNode, true);
            //            zTree_permission_duty_index.addNodes(newParentNode, json.data);
            //$("#rightDetail_Permission_Item_Index").load("/Permission/PermissionItem/Details/" + json.data.Id); //重新加载右侧节点详情
        }
    }

    function refreshTree(asyncUrl) {
        $("#getUrl").html("");
        $("#onAsyncSuccessNode").html("<br/>");
        $("#onAsyncErrorNode").html("<br/>");
        setting_permission_duty_index.asyncUrl = asyncUrl;
        if ((typeof asyncUrl) == "function") {
            $("#asyncUrlCode").html("setAsyncUrl");
        } else {
            $("#asyncUrlCode").html("\"" + asyncUrl + "\"");
        }
        zTree_permission_duty_index = $("#treeduty_permission_duty_index").zTree(setting_permission_duty_index, zNodes_permission_duty_index);
    }
    function dialogAdded_Permission_Duty(json) {
        //提交回调
        //1 接收参数 2 判断成功与否 3 根据返回的数据在树上新增节点
        DWZ.ajaxDone(json);
        if (json.statusCode == DWZ.statusCode.ok) {
            //添加成功
            if (json.callbackType == "Create") {
                var newParentNode = zTree_permission_duty_index.getNodeByParam("ID", json.data.ParentId)
                zTree_permission_duty_index.addNodes(newParentNode, json.data);
            } else if (json.callbackType == "Edit") {
                //更新成功
                var selectedNode = zTree_permission_duty_index.getSelectedNode();
                selectedNode.Name = json.data.Name;
                zTree_permission_duty_index.updateNode(selectedNode, true);
                //更新选中详情
                $("#rightDetail_permission_duty_index").load("/Permission/Duty/ShowDutyDetail/" + selectedNode.ID);
            }
            $.pdialog.closeCurrent();
        }
    }
    //岗位删除回调函数
    function dialogdel_permission_duty_index(json) {
        DWZ.ajaxDone(json);
        if (json.statusCode == DWZ.statusCode.ok) {
            //当前删除节点清除
            var selectedNode = zTree_permission_duty_index.getSelectedNode();
            zTree_permission_duty_index.removeNode(selectedNode, true);
            //当前已选ID清空
            deleteID_permission_duty_index = null;
            //当前详情清空
            $("#addDuty_permission_duty_index").attr("href", href_permission_duty_index + "/CreateDuty/{sid_user}");
            $("#editDuty_permission_duty_index").attr("href", href_permission_duty_index + "/EditDuty/{sid_user}");
            $("#rightDetail_permission_duty_index").html("");
        }
    }
   
</script>
<div class="page">
    @*顶部按钮*@
    <div class="panelBar">
        <ul class="toolBar" id="ul_permission_duty_index">
            <li>@Html.PermissionButton("Permission.Duty.CreateDuty", "addDuty_permission_duty_index", "add", "/Permission/Duty/CreateDuty/{sid_user}", "new_addDuty", "dialog", "", "true","400","280","添加岗位")</li>
            <li>@Html.PermissionButton("Permission.Duty.EditDuty", "editDuty_permission_duty_index", "edit", "/Permission/Duty/EditDuty/{sid_user}", "new_editDuty", "dialog", "", "true", "400", "280","编辑岗位")</li>
            <li class="line">line</li>
            <li>@Html.PermissionButton("Permission.Duty.Delete", "del_permission_duty_index", "delete", "javascript:void(0);", "", "", "", "", "删除")</li>
        </ul>
    </div>
    <div id="mainContent_permission_duty_index" layouth="27">
        <div class="leftPanel">
            @*菜单树*@
            <ul id="treeduty_permission_duty_index" class="ztree">
            </ul>
        </div>
        <div id="rightDetail_permission_duty_index" class="rightPanel">
            @*表单内容*@
            @*<div id="rightDetail_permission_duty_index">
            </div>*@
        </div>
    </div>
</div>
